package com.grass.appointment.fragment;

import android.os.Bundle;
import android.view.View;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;
import com.alibaba.android.arouter.launcher.ARouter;
import com.androidx.p013lv.base.Constance;
import com.androidx.p013lv.base.bean.event.AttentionPhoenixEvent;
import com.androidx.p013lv.base.bean.event.DynamicEvent;
import com.androidx.p013lv.base.http.BaseRes;
import com.androidx.p013lv.base.interfaces.Key;
import com.androidx.p013lv.base.listener.ItemClickListener;
import com.androidx.p013lv.base.p015ui.LazyFragment;
import com.androidx.p013lv.base.utils.ButtonUtil;
import com.androidx.p013lv.base.utils.NetUtil;
import com.androidx.p013lv.base.utils.SpUtils;
import com.androidx.p013lv.base.utils.ToastUtils;
import com.androidx.p013lv.base.utils.UiUtils;
import com.grass.appointment.C2694R;
import com.grass.appointment.adapter.DynamicAdapter;
import com.grass.appointment.bean.AttentionBean;
import com.grass.appointment.bean.DynamicBean;
import com.grass.appointment.bean.DynamicListBean;
import com.grass.appointment.comment.CommentFragment;
import com.grass.appointment.databinding.FragmentDynamicBinding;
import com.grass.appointment.dialog.SelectPicDialog;
import com.grass.appointment.model.DynamicModel;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;
import java.util.Iterator;
import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

public class DynamicFragment extends LazyFragment<FragmentDynamicBinding> implements ItemClickListener, OnRefreshLoadMoreListener, CommentFragment.CommentInterface {
    DynamicAdapter adapter;
    String city;
    private CommentFragment commentFragment;
    SelectPicDialog dialog;
    int index = -1;
    int lastId;
    DynamicModel model;

    public static DynamicFragment newInstance(String str) {
        Bundle bundle = new Bundle();
        bundle.putString(Key.CITY, str);
        DynamicFragment dynamicFragment = new DynamicFragment();
        dynamicFragment.setArguments(bundle);
        return dynamicFragment;
    }

    @Override // com.androidx.p013lv.base.p015ui.LazyFragment
    public int setFragmentView() {
        return C2694R.C2699layout.fragment_dynamic;
    }

    @Override // androidx.fragment.app.Fragment
    public void setArguments(Bundle bundle) {
        super.setArguments(bundle);
        this.city = bundle.getString(Key.CITY);
    }

    @Override // com.androidx.p013lv.base.p015ui.LazyFragment
    public void initViews() {
        EventBus.getDefault().register(this);
        this.model = (DynamicModel) new ViewModelProvider(this).get(DynamicModel.class);
        CommentFragment newInstance = CommentFragment.newInstance();
        this.commentFragment = newInstance;
        newInstance.setCommentInterface(this);
        this.model.data().observe(this, new Observer<BaseRes<DynamicListBean>>() {
            /* class com.grass.appointment.fragment.DynamicFragment.C28091 */

            public void onChanged(BaseRes<DynamicListBean> baseRes) {
                ((FragmentDynamicBinding) DynamicFragment.this.binding).statusLayout.hideLoading();
                ((FragmentDynamicBinding) DynamicFragment.this.binding).refresh.finishRefresh();
                ((FragmentDynamicBinding) DynamicFragment.this.binding).refresh.finishLoadMore();
                if (baseRes.getCode() == 200) {
                    if (baseRes.getData() == null || baseRes.getData().getList() == null || baseRes.getData().getList().size() <= 0) {
                        if (DynamicFragment.this.lastId == 0) {
                            ((FragmentDynamicBinding) DynamicFragment.this.binding).statusLayout.showEmpty();
                        } else {
                            ((FragmentDynamicBinding) DynamicFragment.this.binding).refresh.finishLoadMoreWithNoMoreData();
                        }
                    } else if (DynamicFragment.this.lastId == 0) {
                        DynamicFragment.this.adapter.setDomain(baseRes.getData().getDomain());
                        DynamicFragment.this.adapter.setData(baseRes.getData().getList());
                        ((FragmentDynamicBinding) DynamicFragment.this.binding).refresh.resetNoMoreData();
                    } else {
                        DynamicFragment.this.adapter.setDatasInEnd(baseRes.getData().getList());
                    }
                } else if (DynamicFragment.this.lastId == 0) {
                    ((FragmentDynamicBinding) DynamicFragment.this.binding).statusLayout.showError();
                } else {
                    ToastUtils.getInstance().showWeak(baseRes.getMsg());
                }
            }
        });
        ((FragmentDynamicBinding) this.binding).refresh.setOnRefreshListener(this);
        ((FragmentDynamicBinding) this.binding).refresh.setOnLoadMoreListener(this);
        this.adapter = new DynamicAdapter(this, false);
        ((FragmentDynamicBinding) this.binding).recycler.setLayoutManager(new LinearLayoutManager(getContext()));
        ((FragmentDynamicBinding) this.binding).recycler.setAdapter(this.adapter);
        this.adapter.setItemListener(this);
        ((FragmentDynamicBinding) this.binding).btnSend.setOnClickListener(new View.OnClickListener() {
            /* class com.grass.appointment.fragment.DynamicFragment.View$OnClickListenerC28102 */

            public void onClick(View view) {
                if (DynamicFragment.this.dialog == null) {
                    DynamicFragment.this.dialog = new SelectPicDialog(DynamicFragment.this.getContext(), new View.OnClickListener() {
                        /* class com.grass.appointment.fragment.DynamicFragment.View$OnClickListenerC28102.View$OnClickListenerC28111 */

                        public void onClick(View view) {
                            DynamicFragment.this.dialog.dismiss();
                            if (view.getId() == C2694R.C2697id.ll_video) {
                                ARouter.getInstance().build(Constance.SEND_DYNAMIC_ACTIVITY).withInt("type", 1).navigation();
                            } else if (view.getId() == C2694R.C2697id.ll_pic) {
                                ARouter.getInstance().build(Constance.SEND_DYNAMIC_ACTIVITY).withInt("type", 0).navigation();
                            }
                        }
                    });
                }
                DynamicFragment.this.dialog.show();
            }
        });
        ((FragmentDynamicBinding) this.binding).statusLayout.setOnRetryListener(new View.OnClickListener() {
            /* class com.grass.appointment.fragment.DynamicFragment.View$OnClickListenerC28123 */

            public void onClick(View view) {
                DynamicFragment.this.lastId = 0;
                DynamicFragment.this.getInfo();
            }
        });
        getInfo();
        this.model.like().observe(this, new Observer<BaseRes<String>>() {
            /* class com.grass.appointment.fragment.DynamicFragment.C28134 */

            public void onChanged(BaseRes<String> baseRes) {
                if (baseRes.getCode() == 200) {
                    DynamicBean dynamicBean = (DynamicBean) DynamicFragment.this.adapter.getDataInPosition(DynamicFragment.this.index);
                    dynamicBean.setLike(true);
                    dynamicBean.addFakeLikes();
                    DynamicFragment.this.adapter.notifyItemChanged(DynamicFragment.this.index);
                    return;
                }
                ToastUtils.getInstance().showWeak("喜歡失敗");
            }
        });
        this.model.unLike().observe(this, new Observer<BaseRes<String>>() {
            /* class com.grass.appointment.fragment.DynamicFragment.C28145 */

            public void onChanged(BaseRes<String> baseRes) {
                if (baseRes.getCode() == 200) {
                    DynamicBean dynamicBean = (DynamicBean) DynamicFragment.this.adapter.getDataInPosition(DynamicFragment.this.index);
                    dynamicBean.setLike(false);
                    dynamicBean.unFakeLikes();
                    DynamicFragment.this.adapter.notifyItemChanged(DynamicFragment.this.index);
                    return;
                }
                ToastUtils.getInstance().showWeak("取消喜歡失敗");
            }
        });
        this.model.attention().observe(this, new Observer<BaseRes<AttentionBean>>() {
            /* class com.grass.appointment.fragment.DynamicFragment.C28156 */

            public void onChanged(BaseRes<AttentionBean> baseRes) {
                if (baseRes.getCode() == 200) {
                    DynamicBean dynamicBean = (DynamicBean) DynamicFragment.this.adapter.getDataInPosition(DynamicFragment.this.index);
                    for (DynamicBean dynamicBean2 : DynamicFragment.this.adapter.getData()) {
                        if (baseRes.getData().getBeenUserId() == dynamicBean2.getUserId()) {
                            dynamicBean2.setAttention(true);
                        }
                    }
                    DynamicFragment.this.adapter.notifyDataSetChanged();
                    return;
                }
                ToastUtils.getInstance().showWeak("關注失敗");
            }
        });
        this.model.unAttention().observe(this, new Observer<BaseRes<AttentionBean>>() {
            /* class com.grass.appointment.fragment.DynamicFragment.C28167 */

            public void onChanged(BaseRes<AttentionBean> baseRes) {
                if (baseRes.getCode() == 200) {
                    DynamicBean dynamicBean = (DynamicBean) DynamicFragment.this.adapter.getDataInPosition(DynamicFragment.this.index);
                    for (DynamicBean dynamicBean2 : DynamicFragment.this.adapter.getData()) {
                        if (dynamicBean.getUserId() == dynamicBean2.getUserId()) {
                            dynamicBean2.setAttention(false);
                        }
                    }
                    DynamicFragment.this.adapter.notifyDataSetChanged();
                    return;
                }
                ToastUtils.getInstance().showWeak("取消關注失敗");
            }
        });
    }

    @Override // com.androidx.p013lv.base.listener.ItemClickListener
    public void onItemClick(View view, int i) {
        if (!isOnClick()) {
            this.index = i;
            if (view.getId() == C2694R.C2697id.btn_attention) {
                DynamicBean dynamicBean = (DynamicBean) this.adapter.getDataInPosition(i);
                if (dynamicBean.getUserId() == SpUtils.getInstance().getUserInfo().getUserId()) {
                    ToastUtils.getInstance().showSigh("不能關註自己");
                } else if (!NetUtil.isNetworkAvailable()) {
                    ToastUtils.getInstance().showWrong(UiUtils.getString(C2694R.string.hit_no_net));
                } else if (dynamicBean.isAttention()) {
                    this.model.cancelFollow(dynamicBean.getUserId(), getContext());
                } else {
                    this.model.followUser(dynamicBean.getUserId(), getContext());
                }
            } else if (view.getId() == C2694R.C2697id.tv_approve) {
                if (!NetUtil.isNetworkAvailable()) {
                    ToastUtils.getInstance().showWrong(UiUtils.getString(C2694R.string.hit_no_net));
                    return;
                }
                DynamicBean dynamicBean2 = (DynamicBean) this.adapter.getDataInPosition(i);
                if (dynamicBean2.isLike()) {
                    this.model.likeUnDynamic(dynamicBean2.getDynamicId(), getContext());
                } else {
                    this.model.likeDynamic(dynamicBean2.getDynamicId(), getContext());
                }
            } else if (view.getId() == C2694R.C2697id.tv_comment) {
                if (!ButtonUtil.isFastDoubleClick(1000)) {
                    this.commentFragment.loadDynamicComment(((DynamicBean) this.adapter.getDataInPosition(i)).getUserId(), ((DynamicBean) this.adapter.getDataInPosition(i)).getDynamicId(), ((DynamicBean) this.adapter.getDataInPosition(i)).getCommentNum(), i);
                    this.commentFragment.show(getChildFragmentManager(), "CommentFragment");
                }
            } else if (view.getId() == C2694R.C2697id.iv_head) {
                ARouter.getInstance().build(Constance.USER_INFO_ACTIVITY).withInt(Key.USER_ID, ((DynamicBean) this.adapter.getDataInPosition(i)).getUserId()).withInt("type", 1).navigation();
            }
        }
    }

    /* access modifiers changed from: package-private */
    public void getInfo() {
        if (this.lastId == 0) {
            DynamicAdapter dynamicAdapter = this.adapter;
            if (!(dynamicAdapter == null || dynamicAdapter.getData() == null || this.adapter.getData().size() <= 0)) {
                this.adapter.clear();
            }
            if (!NetUtil.isNetworkAvailable()) {
                ((FragmentDynamicBinding) this.binding).statusLayout.showNoNet();
                return;
            }
            ((FragmentDynamicBinding) this.binding).statusLayout.showLoading();
        }
        this.model.getInfo(-1, this.lastId);
    }

    @Override // com.grass.appointment.comment.CommentFragment.CommentInterface
    public void addCommentNum(int i, int i2) {
        ((DynamicBean) this.adapter.getData().get(i)).setCommentNum(i2);
        this.adapter.notifyItemChanged(i, Integer.valueOf(C2694R.C2697id.tv_comment));
    }

    @Override // com.scwang.smartrefresh.layout.listener.OnLoadMoreListener
    public void onLoadMore(RefreshLayout refreshLayout) {
        DynamicAdapter dynamicAdapter = this.adapter;
        if (dynamicAdapter == null || dynamicAdapter.getData() == null || this.adapter.getData().size() == 0) {
            ((FragmentDynamicBinding) this.binding).refresh.finishLoadMore();
            return;
        }
        this.lastId = ((DynamicBean) this.adapter.getLastDataInPosition()).getDynamicId();
        getInfo();
    }

    @Override // com.scwang.smartrefresh.layout.listener.OnRefreshListener
    public void onRefresh(RefreshLayout refreshLayout) {
        this.lastId = 0;
        getInfo();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onFansOrFollowEvent(AttentionPhoenixEvent attentionPhoenixEvent) {
        if (attentionPhoenixEvent != null) {
            for (DynamicBean dynamicBean : this.adapter.getData()) {
                if (attentionPhoenixEvent.getUserId() == dynamicBean.getUserId()) {
                    dynamicBean.setAttention(attentionPhoenixEvent.isAttention());
                }
            }
            this.adapter.notifyDataSetChanged();
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onLikeDynamic(DynamicEvent dynamicEvent) {
        if (dynamicEvent != null) {
            Iterator it = this.adapter.getData().iterator();
            while (true) {
                if (!it.hasNext()) {
                    break;
                }
                DynamicBean dynamicBean = (DynamicBean) it.next();
                if (dynamicBean.getDynamicId() == dynamicEvent.getDynamicId()) {
                    dynamicBean.setLike(dynamicEvent.isLike());
                    dynamicBean.setFakeLikes(dynamicEvent.getFakeLikes());
                    break;
                }
            }
            this.adapter.notifyDataSetChanged();
        }
    }

    @Override // androidx.fragment.app.Fragment, com.gyf.immersionbar.components.ImmersionFragment
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
        DynamicModel dynamicModel = this.model;
        if (dynamicModel != null) {
            dynamicModel.cancelHttp();
        }
    }
}
